package edu.bw.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import po.Order;
import po.User;

import java.util.List;

public interface OrderMapper {
    /**
     * 查询所有订单(延迟加载订单关联的用户信息)
     * @return
     */
    @Results({
            @Result(column = "user_id",property = "userId"),
            @Result(column = "user_id",property = "user" ,javaType = User.class,
                    one = @One(select = "edu.bw.mapper.UserMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("SELECT * FROM mybatis02.`order`")
    List<Order> findOrderAndUserLazy();

    /**
     * 查询所有订单(延迟加载订单关联的用户信息和订单关联的订单明细)
     * //@Many相当于collection标签
     * @return
     */
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "user_id",property = "user" ,javaType = User.class,
                    one = @One(select = "edu.bw.mapper.UserMapper.findById",fetchType = FetchType.LAZY)),
            @Result(column = "id",property = "orderDetails",javaType = List.class,//当返回的是集合时指定为List.class
                    many = @Many(select = "edu.bw.mapper.OrderDetailMapper.findOrderDetailByOrderId",fetchType = FetchType.LAZY))
    })
    @Select("SELECT id,user_id,number,createtime,note FROM mybatis02.`order`")
    List<Order> findOrdersAndOrderDetailLazy();

    /**
     * 查询所有订单(每个订单关联用户,每个订单关联着订单明细)
     * @return
     */
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(property = "orderDetails",javaType = List.class,column = "id",
            many = @Many(select = "edu.bw.mapper.OrderDetailMapper.findOrderDetailByOrderId",fetchType = FetchType.LAZY)),
    })
    @Select("SELECT id, user_id, number, createtime, note FROM mybatis02.`order`")
    List<Order> findOrdersByUserId();
}
